package com.c1g19.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.json.JSONArray;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.c1g19.utils.JsonUtils;

public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(BaseAction.class);
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Integer jtStartIndex = 1;
	private Integer jtPageSize;
	private Integer jtSorting;

	/**
	 * 设置HttpServletRequest
	 */
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	/**
	 * 设置HttpServletResponse
	 */
	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	/**
	 * 获取HttpServletRequest
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * 获取HttpServletResponse
	 */
	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * 设置RequestAttribute
	 * 
	 * @param name
	 *            参数名
	 * @param value
	 *            参数值
	 */
	public void setRequestAttribute(String name, Object value) {
		request.setAttribute(name, value);
	}

	/**
	 * 设置SessionAttribute
	 * 
	 * @param name
	 *            参数名
	 * @param value
	 *            参数值
	 */
	public void setSessionAttribute(String name, Object value) {
		request.getSession().setAttribute(name, value);
	}

	/**
	 * 获取SessionAttribute
	 * 
	 * @param name
	 *            参数名
	 * @param value
	 *            参数值
	 */
	public Object getSessionAttribute(String key) {
		return request.getSession().getAttribute(key);
	}

	/**
	 * 获取RequestAttribute
	 * 
	 * @param name
	 *            参数名
	 * @param value
	 *            参数值
	 */
	public Object getRequestAttribute(String key) {
		return request.getAttribute(key);
	}

	public Integer getJtPageSize() {
		return jtPageSize;
	}

	public void setJtPageSize(Integer jtPageSize) {
		this.jtPageSize = jtPageSize;
	}

	public Integer getJtStartIndex() {
		return jtStartIndex;
	}

	public void setJtStartIndex(Integer jtStartIndex) {
		this.jtStartIndex = jtStartIndex;
	}

	public Integer getJtSorting() {
		return jtSorting;
	}

	public void setJtSorting(Integer jtSorting) {
		this.jtSorting = jtSorting;
	}

	// 将数据列表转换成Jtable控件所需的Json字符串
	public String changeListToJsonStringWithJtable(Map<String, Object> dataMap) {
		JSONObject jsonObject = new JSONObject();
		try {
			List<Map<String, Object>> newsList = (List<Map<String, Object>>) dataMap
					.get("dataList");
			long totalCount = (Long) dataMap.get("totalCount");
			// 将List转为Json字符串
			String res = JsonUtils.listO2Json(newsList);
			JSONArray resArray;
			resArray = new JSONArray(res);
			jsonObject.put("Result", "OK");
			jsonObject.put("Records", resArray);
			jsonObject.put("TotalRecordCount", totalCount);
		} catch (Exception e) {
			logger.error("", e);
		}
		return jsonObject.toString();
	}
}
